/*Copyright*/
package com.jmetest.physics.ragdoll;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.jme.bounding.BoundingBox;
import com.jme.input.InputHandler;
import com.jme.input.KeyInput;
import com.jme.input.MouseInput;
import com.jme.input.action.InputAction;
import com.jme.input.action.InputActionEvent;
import com.jme.math.Vector3f;
import com.jme.scene.Text;
import com.jme.scene.shape.Box;
import com.jmex.physics.StaticPhysicsNode;
import com.jmex.physics.util.PhysicsPicker;
import com.jmex.physics.util.SimplePhysicsGame;

/**
 * A short test for the {@link SimpleRagDoll} class.
 * @author mud2005, irrisor
 */
public class TestSimpleRagDoll extends SimplePhysicsGame {

    protected void simpleInitGame() {
        addFloor();
        configurePhysicsPicker();
        adjustCameraView();
        registerCreateRagDollAction();

        Text label2 = Text.createDefaultTextLabel( "instructions", "Hit [space] to drop a ragdoll. [v] to toggle physics view." );
        label2.setLocalTranslation( 0, 20, 0 );
        fpsNode.attachChild( label2 );
    }

    private void addFloor() {
        StaticPhysicsNode staticNode = getPhysicsSpace().createStaticNode();

        Box floorBox = new Box( "", new Vector3f(), 100, 1, 100 );
        floorBox.setModelBound( new BoundingBox() );
        floorBox.updateModelBound();
        staticNode.attachChild( floorBox );

        rootNode.attachChild( staticNode );

        staticNode.getLocalTranslation().set( 0, -10, 0 );
        staticNode.updateGeometricState( 0, false );

        staticNode.generatePhysicsGeometry();
    }

    private void configurePhysicsPicker() {
        cameraInputHandler.setEnabled( false );
        new PhysicsPicker( input, rootNode, getPhysicsSpace(), true );
        MouseInput.get().setCursorVisible( true );
    }

    private void adjustCameraView() {
        cam.getLocation().y += 10;
        cam.getLocation().z += 10;
        cam.lookAt( new Vector3f(), new Vector3f( 0, 0, -1 ) );
    }

    private void registerCreateRagDollAction() {
        InputAction createAction = new InputAction() {
            public void performAction( InputActionEvent evt ) {
                if ( evt == null || evt.getTriggerPressed() ) {
                    SimpleRagDoll sr2 = new SimpleRagDoll( getPhysicsSpace() );
                    sr2.getRagdollNode().getLocalTranslation().y += 20;
                    rootNode.attachChild( sr2.getRagdollNode() );
                    rootNode.updateGeometricState( 0, false );
                    rootNode.updateRenderState();
                }
            }
        };
        input.addAction( createAction, InputHandler.DEVICE_KEYBOARD, KeyInput.KEY_SPACE, InputHandler.AXIS_NONE, false );
        createAction.performAction( null );
    }

    public static void main( String[] args ) {
        Logger.getLogger( "" ).setLevel( Level.WARNING );
        final TestSimpleRagDoll app = new TestSimpleRagDoll();
//        app.setDialogBehaviour( AbstractGame.ALWAYS_SHOW_PROPS_DIALOG );
        new Thread() {
            @Override
            public void run() {
                app.start();
            }
        }.start();
    }
}

/*
 * $Log: not supported by cvs2svn $
 */

